package com.sth.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sth.commonutils.JwtUtils;
import com.sth.commonutils.R;
import com.sth.order.entity.Order;
import com.sth.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author sth
 * @since 2020-12-20
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    //1.根据课程id和用户id创建订单，返回订单id
    @PostMapping("createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        String orderNo = orderService.saveOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId", orderNo);
    }

    //2.根据订单id查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item",order);
    }
    // 统计某一天的注册人数
    @PostMapping("registerCount/{day}")
    public R registerCount(@PathVariable String day) {
        orderService.registerCount(day);
        return R.ok();
    }

    // test
    @GetMapping("test/{day}")
    public R test(@PathVariable String day) {
        orderService.test(day);
        return R.ok();
    }
}

